package com.linkpow.smartelevator.common;

import java.lang.reflect.Field;

/**
 * @ClassName ErrorMessage
 * @Description 常见错误消息定义
 * @Author 王丹
 * @Date 2019/11/25 13:57
 * @Version 1.0
 */
public class ErrorMessage {

    public static final ErrorMessage SUCCESS = new ErrorMessage("000000", "成功");
    public static final ErrorMessage ERROR_VALIDATION = new ErrorMessage("999888", "数据验证失败");
    public static final ErrorMessage FAIL = new ErrorMessage("999999", "失败");

    protected String messageCode;
    protected String messageValue;

    public ErrorMessage(String code, String value) {
        this.messageCode = code;
        this.messageValue = value;
    }

    public static ErrorMessage valueOf(String code) {

        try {
            Class<ErrorMessage> c = ErrorMessage.class;
            Field[] fs = c.getFields();
            for (Field f : fs) {
                if (f.getName().equals(code)) {
                    return (ErrorMessage) f.get(new ErrorMessage("", ""));
                }
            }
        } catch (Exception e) {
        }
        return null;
    }

    public String name() {

        return messageCode;
    }

    public String getMessageCode() {

        return messageCode;
    }

    public String getMessageValue() {

        return messageValue;
    }

    public boolean isSuccess() {

        boolean ret = false;
        if (ErrorMessage.SUCCESS.getMessageCode().equals(this.messageCode)) {
            ret = true;
        }
        return ret;
    }

    @Override
    public String toString() {

        return this.messageCode;
    }

    public static boolean isSuccess(String code) {

        boolean ret = false;
        if (ErrorMessage.SUCCESS.getMessageCode().equals(code)) {
            ret = true;
        }
        return ret;
    }
}
